//https://leetcode.cn/problems/find-all-anagrams-in-a-string/submissions/550798166/

class Solution {
public:
    vector<int> findAnagrams(string s, string p) {
        vector<int> ret;
        int hash1[26] = { 0 };
        int hash2[26] = { 0 }; 
        for (auto e : p)
        {
            hash1[e - 'a']++;
        }
        int m = p.size();
        for (int left = 0, right = 0, count = 0; right < s.size(); right++)
        {
            //进窗口
            hash2[s[right] - 'a']++;
            if (hash2[s[right] - 'a'] <= hash1[s[right] - 'a'])
                count++;
            //判断
            if (right - left + 1 > m)
            {
                if (hash2[s[left] - 'a'] <= hash1[s[left] - 'a'])
                    count--;
                //出窗口
                hash2[s[left++] - 'a']--;

            }
            //更新结果
            if (count == m)
                ret.push_back(left);
        }

        return ret;

    }
};